package com.dg.river.core.util;

import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ScaleXSpan;
import android.util.Log;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by hy on 2018/10/16
 * Description: 文字工具类
 */
public class TextUtil {
    /**
     * @MethodName isNotEmpty
     * @Description 判断字符串是否不为空
     * @Param @param str
     * @Param @return
     * @Return boolean
     * @Throws
     */
    public static boolean isNotEmpty(String str) {
        if (null != str && str.trim().length() > 0) {
            return true;
        }
        return false;
    }

    /**
     * 比较两个字符串是否相等
     *
     * @param str1
     * @param str2
     * @return
     */
    public static final boolean isEqual(String str1, String str2) {
        if (str1 == null || "".equals(str1.trim())) {
            return false;
        }
        if (str2 == null || "".equals(str2.trim())) {
            return false;
        }
        return str1.equals(str2);
    }

    /**
     * 判断传过来的字符串是否为空
     *
     * @param str
     * @return
     */
    public static boolean isEmpty(String str) {
        if (str != null && !"".equals(str.trim())) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 判断传过来的字符串是否为空，如果为空则返回空字符串
     *
     * @param str
     * @return
     */
    public static String isEmptyConvert(String str) {
        if (str != null && !"".equals(str.trim())) {
            return str.trim();
        } else {
            return "";
        }
    }

    /**
     * 判断传过来的字符串是否为空，如果为空则返回空字符串
     *
     * @param str
     * @return
     */
    public static String isEmptyConvert(String str, String replace) {
        if (str != null && !"".equals(str.trim())) {
            return str.trim();
        } else {
            return replace;
        }
    }

    /**
     * 判断传过来的字符串是否为空，如果为空则返回空字符串
     *
     * @param str
     * @return
     */
    public static String isEmptyConvertNoTrim(String str) {
        if (str != null && !"".equals(str)) {
            return str;
        } else {
            return "";
        }
    }

    /**
     * 字符串替换-将一些转义符替换
     *
     * @param string
     * @return
     */
    public static String replaceStr(String string) {
        return string.replace("&ldquo;", "“")
                .replace("&rdquo;", "”")
                .replace("&quot;", "\"")
                .replace("&gt;", ">")
                .replace("&lt;", "<")
                .replace("&amp;", "&")
                .replace("&hellip;", "...")
                .replace("/&middot;", ".");
    }

    /**
     * 隐藏字符串
     *
     * @param string     待替换的字符串
     * @param startCount 开始保留的位数
     * @param endCount   结尾保留的位数
     * @return
     */
    public static String hideStr(String string, int startCount, int endCount) {
        if (string.length() < startCount || string.length() < endCount) {
            return string;
        } else if ((string.length() - endCount) < startCount) {
            return string;
        } else {
            String startStr = string.substring(0, startCount);
            String endStr = string.substring(string.length() - endCount);
            return startStr + "****" + endStr;
        }
    }

    /**
     * 将给定的字符串给定的长度两端对齐
     *
     * @param str  待对齐字符串
     * @param size 汉字个数，eg:size=5，则将str在5个汉字的长度里两端对齐
     * @return
     */
    public static SpannableStringBuilder justifyString(String str, int size) {
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
        if (TextUtils.isEmpty(str)) {
            return spannableStringBuilder;
        }
        char[] chars = str.toCharArray();
        if (chars.length >= size || chars.length == 1) {
            return spannableStringBuilder.append(str);
        }
        int l = chars.length;
        float scale = (float) (size - l) / (l - 1);
        for (int i = 0; i < l; i++) {
            spannableStringBuilder.append(chars[i]);
            if (i != l - 1) {
                SpannableString s = new SpannableString("　");//全角空格
                s.setSpan(new ScaleXSpan(scale), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableStringBuilder.append(s);
            }
        }
        return spannableStringBuilder;
    }

    /**
     * 判断是否包含特殊字符
     *
     * @return false:未包含 true：包含
     */
    public static boolean inputJudge(String editText) {
        String speChat = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";
        Pattern pattern = Pattern.compile(speChat);
        Log.d("inputJudge", "pattern: " + pattern);
        Matcher matcher = pattern.matcher(editText);
        Log.d("inputJudge", "matcher: " + matcher);
        if (matcher.find()) {
            return true;
        } else {
            return false;
        }
    }
}
